package com.example.industry.controller;

import com.example.industry.entity.User;
import com.example.industry.enums.Roles;
import com.example.industry.service.ProjectService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/project")
public class ProjectController
{
    @Resource
    private ProjectService projectService;

    /**
     * 创建项目
     * @param projectName 项目名称
     * @param chairmanId 创建者Id
     * @param projectIntroduction 项目介绍
     * @return 是否成功
     */
    @PostMapping(value = "/create")
    public boolean createProject(@RequestParam(value = "projectName") String projectName,
                                 @RequestParam(value = "chairmanId") Integer chairmanId,
                                 @RequestParam(value = "projectIntroduction") String projectIntroduction)
    {
        return projectService.createProject(projectName, chairmanId, projectIntroduction);
    }

    /**
     * 删除项目
     * @param projectId 项目Id
     * @return 是否成功
     */
    @DeleteMapping(value = "/{projectId}")
    public boolean deleteProject(@PathVariable Integer projectId)
    {
        return projectService.deleteProjectById(projectId);
    }

    /**
     * 更新项目
     * @param projectId 项目Id
     * @param projectName 项目名
     * @param projectIntroduction 项目介绍
     * @return 是否成功
     */
    @PutMapping(value = "/update")
    public boolean updateProject(@RequestParam(value = "projectId") Integer projectId,
                                 @RequestParam(value = "projectName") String projectName,
                                 @RequestParam(value = "projectIntroduction") String projectIntroduction)
    {
        return projectService.updateProject(projectId, projectName, projectIntroduction);
    }

    /**
     * 获取一个项目中的成员, 指定角色
     * @param projectId 项目Id
     * @param role 角色
     * @return 用户集合
     */
    @GetMapping(value = "/users")
    public List<User> findUsersByProjectIdAndRole(@RequestParam(value = "projectId") Integer projectId,
                                                  @RequestParam(value = "role") Roles role)
    {
        return projectService.findUsersByProjectIdAndRole(projectId, role);
    }

    /**
     * 删除项目中的成员
     * @param projectId 项目Id
     * @param userId 用户Id
     * @return 是否成功
     */
    @DeleteMapping(value = "/delete_user")
    public boolean deleteUserInProjectByUserId(@RequestParam(value = "projectId") Integer projectId,
                                               @RequestParam(value = "userId") Integer userId)
    {
        return projectService.deleteUserInProjectByUserId(projectId, userId);
    }
}
